﻿#region References

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;


#endregion

#region Class

/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page
{
    //private bool terminateEvents = false;

    public BasePage()
    {
        //
        // TODO: Add constructor logic here
        //
        //WebUtil.LoginUser();
    }

    #region Overridden Page load event
    /// <summary>
    /// Handles page load event
    /// </summary>
    /// <param name="e"></param>
    protected override void OnLoad(EventArgs e)
    {
        //List<App.Models.Admin.AdminGroupTable> groups = ApplicationCache.UserGroups;
        WebUtil.LoginUser();
        //if (!HasAccessPermission())
        //{
        //    Response.Redirect(AppConstants.Pages.ACCESS_DENIED);
        //    return;
        //}
        base.OnLoad(e);
    }
    /// <summary>
    /// Determines the Access Permission for Currently Requested Page.
    /// There is no need to do this, because our objective is to disable the user from changing any data.
    /// So, we don't need to use this method.
    /// </summary>
    /// <returns></returns>
    //protected bool HasAccessPermission()
    //{
    //    if (IsPublicPage())
    //        return true;

    //    if (WebUtil.IsAdminUser())
    //        return true;
    //    else if (SessionCache.CurrentUser != null)
    //    {
    //        ///Group and Permissions are Defined for this user
    //        if (SessionCache.CurrentUser.Group != null && SessionCache.CurrentUser.Group.Permissions != null)
    //        {
    //            McAIDNavigationParentMenuMenuGroupChildMenu siteMapNode = WebUtil.GetCurrentSiteMapNode();
    //            //String winFormName = siteMapNode == null ? "" : siteMapNode.WinFormName;
    //            var permission = SessionCache.CurrentUser.Group.Permissions.SingleOrDefault(P => P.FrmName == siteMapNode.WinFormName);
    //            if (permission == null)
    //                return false;                
    //        }
    //        ///Group and Permissions are not defined
    //        else
    //        {
    //            ///This situation will never happen, according to Nazmul Vai. He said that, every user will must be assigned to a Group
    //            ///And All group wull must have Group Permissions defined.
    //            ///So, for the development time we are giving access to those users who does not have Group/Permission set defined.
    //            return true;
    //        }
    //    }
    //    return true;
    //}
    //private bool IsPublicPage()
    //{
    //    if (Request.Url.AbsolutePath.ToLower().IndexOf("/public/") > -1)
    //        return true;
    //    else if (Request.Url.AbsolutePath.ToLower().IndexOf(AppConstants.Pages.PERFORMENCE_TABLE.ToLower()) > -1)
    //        return true;
    //    else if (Request.Url.AbsolutePath.ToLower().IndexOf("/private/") > -1)
    //        return false;
    //    else
    //        return true;
    //}
    #endregion

    #region ViewState Compression and Decompresson
    /// <summary>
    /// Load and Decompress ViewState
    /// </summary>
    /// <returns></returns>
    protected override object LoadPageStateFromPersistenceMedium()
    {
        //return base.LoadPageStateFromPersistenceMedium();
        string viewState = Request.Form["__VSTATE"];
        byte[] bytes = Convert.FromBase64String(viewState);
        bytes = Compressor.Decompress(bytes);
        LosFormatter formatter = new LosFormatter();
        return formatter.Deserialize(Convert.ToBase64String(bytes));
    }
    /// <summary>
    /// Compress and Save ViewState
    /// </summary>
    /// <param name="viewState"></param>
    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
        //base.SavePageStateToPersistenceMedium(state);
        LosFormatter formatter = new LosFormatter();
        StringWriter writer = new StringWriter();
        formatter.Serialize(writer, viewState);
        string viewStateString = writer.ToString();
        byte[] bytes = Convert.FromBase64String(viewStateString);
        bytes = Compressor.Compress(bytes);
        ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
    }
    #endregion

    #region Signout user
    /// <summary>
    /// Signouts the user.
    /// </summary>
    protected void SignoutUser()
    {
        WebUtil.SignoutUser();
    }
    #endregion
    #region Properties
    public bool IsAdministrator
    {
        get
        {
            //if (SessionCache.CurrentUser != null)
            //    return SessionCache.CurrentUser.IsAdmin;
            
            return false;
        }
    }
    #endregion
    #region User Permission
    /// <summary>
    /// Checks Whether the Currentyl Logged In User Has Delete Permission
    /// </summary>
    //protected bool HasDeletePermission
    //{
    //    get
    //    {
    //        return WebUtil.HasDeletePermission();
    //    }
    //}
    ///// <summary>
    ///// Checks Whether the Currently Logged In User has Edit Permission
    ///// </summary>
    //protected bool HasEditPermission
    //{
    //    get
    //    {
    //        return WebUtil.HasEditPermission();
    //    }
    //}
    //protected bool HasReadOnlyPermission
    //{
    //    get
    //    {
    //        return WebUtil.HasReadOnlyPermission();
    //    }
    //}
    #endregion

    //protected void CheckButtonPermissions(Button btnSave, Button btnDelete)
    //{
    //    if (btnSave != null && !HasEditPermission)
    //        btnSave.Enabled = false;

    //    if (btnDelete != null && !HasDeletePermission)
    //        btnDelete.Enabled = false;
    //}
}

#endregion